package com.genesis.yunnanji.fragment;

import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;

import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import org.xutils.x;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.genesis.yunnanji.R;
import com.genesis.yunnanji.activity.DiZhouDetailsActivity;
import com.genesis.yunnanji.activity.WebActivity;
import com.genesis.yunnanji.adapter.PavilionDateAdapter;
import com.genesis.yunnanji.bean.PavilionBean;
import com.genesis.yunnanji.bean.SpecialtyBannerBean;
import com.genesis.yunnanji.config.GenesisApiConfig;
import com.genesis.yunnanji.utils.NetWorkUtils;
import com.google.gson.Gson;
import com.lcodecore.tkrefreshlayout.TwinklingRefreshLayout;
import com.youth.banner.Banner;
import com.youth.banner.BannerConfig;
import com.youth.banner.listener.OnBannerListener;
import com.youth.banner.loader.ImageLoader;
import com.yqritc.recyclerviewflexibledivider.VerticalDividerItemDecoration;

import org.json.JSONObject;
import org.xutils.http.RequestParams;
import org.xutils.image.ImageOptions;

import java.util.ArrayList;
import java.util.List;


public class PavilionFragment extends Fragment {

    private List<SpecialtyBannerBean.ResultBean.ListBean> bannerList = new ArrayList<>();//后台返回banner图

    private RecyclerView rvPavilion;
    private Banner pavilionBanner;
    private int page = 1;
    private List<PavilionBean.ResultBean.ListBean> list;
    private View view;
    private PavilionDateAdapter mPavilionDateAdapter;

    public static PavilionFragment newInstance(String param) {
        PavilionFragment obj = new PavilionFragment();
        Bundle bundle = new Bundle();
        bundle.putString("args1", param);
        obj.setArguments(bundle);
        return obj;
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        if (view == null) {
            view = this.getActivity().getLayoutInflater().inflate(R.layout.fragment_pavilion, null);
            rvPavilion = (RecyclerView) view.findViewById(R.id.rvPavilion);
            pavilionBanner = view.findViewById(R.id.pavilionBanner);
            netBanner();//加载banner
            initView();
        } else {
            ViewGroup parent = (ViewGroup) view.getParent();
            if (parent != null) {
                parent.removeView(view);
            }
        }
        return view;
    }

    protected void initView() {

    }

    private void netBanner() {
        final RequestParams params = new RequestParams(GenesisApiConfig.HOST + GenesisApiConfig.BANNER_ADD);
        params.addBodyParameter("code", "app_dizhou_banner");
        NetWorkUtils.doCacheGet(params, new NetWorkUtils.HttpResultListener() {
            @Override
            public void onSuccess(JSONObject jsonObject) {
                Gson gson = new Gson();
                SpecialtyBannerBean bean = gson.fromJson(jsonObject.toString(), SpecialtyBannerBean.class);
                bannerList = bean.getResult().getList();
                initBanner();
            }

            @Override
            public void OnCached(JSONObject jsonObject) {
            }

            @Override
            public void onFinished() {
            }

            @Override
            public void onError(String errorStr) {
            }
        });
    }

    private void initBanner() {
        if (bannerList.size() <= 0 || bannerList == null) {
            pavilionBanner.setVisibility(View.GONE);
        } else {
            pavilionBanner.setVisibility(View.VISIBLE);
            List<String> bannerImages = new ArrayList<>();
            for (int i = 0; i < bannerList.size(); i++) {
                bannerImages.add(bannerList.get(i).getAdvert_img());
            }
            pavilionBanner.setBannerStyle(BannerConfig.CIRCLE_INDICATOR);
            pavilionBanner.setImageLoader(new ImageLoader() {
                @Override
                public void displayImage(Context context, Object path, ImageView imageView) {
                    ImageOptions imageOptions = new ImageOptions.Builder().setImageScaleType(ImageView.ScaleType.FIT_XY).build();
                    x.image().bind(imageView, GenesisApiConfig.PIC_HOST + (String) path, imageOptions);
                }
            });
            pavilionBanner.setOnBannerListener(new OnBannerListener() {
                @Override
                public void OnBannerClick(int position) {
                    if (bannerList.get(position).getTarget_link().equals("")) {
//                        Toast.makeText(getActivity(), "网络链接走丢了!", Toast.LENGTH_SHORT).show();
                    } else {
                        Intent intent = new Intent(getActivity(), WebActivity.class);
                        intent.putExtra("url", bannerList.get(position).getTarget_link());
                        startActivity(intent);
                    }
                }
            });
            pavilionBanner.setIndicatorGravity(BannerConfig.CENTER);
            pavilionBanner.isAutoPlay(true);
            pavilionBanner.setDelayTime(5000);
            pavilionBanner.setImages(bannerImages);
            pavilionBanner.start();
        }
        getData(null);
    }

    private void getData(final TwinklingRefreshLayout refreshLayout) {
//        RequestParams params = new RequestParams(GenesisApiConfig.HOST + GenesisApiConfig.PAVILION_LIST);
        RequestParams params = new RequestParams(GenesisApiConfig.HOST + GenesisApiConfig.DIZHOU_NEW_INDEX);
//        params.addBodyParameter("p", 1 + "");
        NetWorkUtils.doCacheGet(params, new NetWorkUtils.HttpResultListener() {
            @Override
            public void onSuccess(JSONObject jsonObject) {
                Gson gson = new Gson();
                PavilionBean pavilionBean = gson.fromJson(jsonObject.toString(), PavilionBean.class);
                final List<PavilionBean.ResultBean.ListBean> list = pavilionBean.getResult().getList();
                mPavilionDateAdapter = new PavilionDateAdapter(getActivity(), list);
                LinearLayoutManager layoutManager = new LinearLayoutManager(getActivity());
                //设置水平滚动
                layoutManager.setOrientation(LinearLayoutManager.HORIZONTAL);
                rvPavilion.setLayoutManager(layoutManager);
                rvPavilion.addItemDecoration(new VerticalDividerItemDecoration.Builder(getActivity()).size((int) getResources().getDimension(R.dimen.x10)).color(Color.parseColor("#f5f5f5")).build());
                rvPavilion.setAdapter(mPavilionDateAdapter);
                mPavilionDateAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
                    @Override
                    public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                        Intent intent = new Intent(getActivity(), DiZhouDetailsActivity.class);
                        intent.putExtra("id", list.get(position).getId());
                        startActivity(intent);
                    }
                });
            }

            @Override
            public void OnCached(JSONObject jsonObject) {
            }

            @Override
            public void onFinished() {
            }

            @Override
            public void onError(String errorStr) {

            }
        });
    }

}
